<template>
  <div>
    <div class="dialog-wrap linkagepopup-warp">
      <div class="dialog-cover" v-if="isShow" ></div>
      <div class="dialog-content" v-if="isShow">
          <div class="devicepopup">
              <div class="popupheader">
                新增联动
                <el-button type="primary" icon="el-icon-close" @click="linkageclose" size="mini" ></el-button>
              </div>
              <div class="popupcontent">
                    <ul>
                        <li class="border">
                            <div class="popupcontent-title">告警设备管理</div>
                            <div class="popupcontent-mian">
                                  <div class="link-popu-box">
                                      <span class="popu-box">告警设备</span>
                                      <el-select v-model="alarmsid" @change="clear_alarmname"  placeholder="请选择" size="mini" >
                                          <el-option
                                            v-for="item in alarmList"
                                            :label="item.sname"
                                            :value="item.sid">
                                          </el-option>
                                      </el-select>
                                  </div>
                                  <div class="link-popu-box">
                                      <span class="popu-box">告警设备名称</span>
                                      <el-select v-model="alarmName"  @change="get_alarm_brief"placeholder="请选择" size="mini">
                                          <el-option
                                            v-for="item in alarmNameList[alarmsid]"
                                            :label="item"
                                            :value="item">
                                          </el-option>
                                      </el-select>
                                  </div>
                                  <div class="link-popu-box">
                                    <span class="popu-box">告警设备指令</span>
                                    <el-select v-model="alarmbriefid" placeholder="请选择" size="mini">
                                        <el-option
                                          v-for="item in alarmBriefList"
                                          :key="item.alarmbriefid"
                                          :label="item.alarmbrief"
                                          :value="item.alarmbriefid">
                                        </el-option>
                                    </el-select>
                                  </div>
                            </div>
                        </li>
                        <li class="border">
                            <div class="popupcontent-title">联动设备管理</div>
                            <div class="link-popu-box">
                                <span class="popu-box">联动设备</span>
                                <el-select  v-model="ctrlsid"  @change="clear_ctrlname" placeholder="请选择" size="mini">
                                    <el-option
                                      v-for="item in ctrlList"
                                      :key="item.sid"
                                      :label="item.sname"
                                      :value="item.sid">
                                    </el-option>
                                </el-select>
                            </div>
                            <div class="link-popu-box">
                                <span class="popu-box">联动设备名称</span>
                                <el-select v-model="ctrlName" @change="get_ctrl_brief" placeholder="请选择" size="mini" >
                                    <el-option
                                      v-for="item in ctrlNameList[ctrlsid]"
                                      :key="item"
                                      :label="item"
                                      :value="item">
                                    </el-option>
                                </el-select>
                            </div>
                            <div class="link-popu-box">
                                <span class="popu-box">联动指令</span>
                                <el-select v-model="ctrlbriefid" placeholder="请选择" size="mini">
                                    <el-option
                                      v-for="item in ctrlBriefList"
                                      :key="item.ctrlbriefid"
                                      :label="item.ctrlbrief"
                                      :value="item.ctrlbriefid">
                                    </el-option>
                                </el-select>
                            </div>
                        </li>
                        <li>
                            <div class="popupcontent-title">解除设备管理</div>
                            <div class="link-popu-box">
                                <span class="popu-box">解除设备</span>
                                <el-select  v-model="freesid" @change="clear_freename" placeholder="请选择" size="mini">
                                    <el-option
                                      v-for="item in freeList"
                                      :key="item.sid"
                                      :label="item.sname"
                                      :value="item.sid">
                                    </el-option>
                                </el-select>
                            </div>
                            <div class="link-popu-box">
                                <span class="popu-box">解除设备名称</span>
                                <el-select v-model="freeName" @change="get_free_brief" placeholder="请选择" size="mini" >
                                    <el-option
                                      v-for="item in freeNameList[freesid]"
                                      :key="item"
                                      :label="item"
                                      :value="item">
                                    </el-option>
                                </el-select>
                            </div>
                            <div class="link-popu-box">
                                <span class="popu-box">解除指令</span>
                                <el-select v-model="freebriefid" placeholder="请选择" size="mini">
                                    <el-option
                                      v-for="item in freeBriefList"
                                      :key="item.freebriefid"
                                      :label="item.freebrief"
                                      :value="item.freebriefid">
                                    </el-option>
                                </el-select>
                            </div>
                        </li>
                    </ul>
              </div>
              <div class="popupfooter">
                <el-button type="primary" size="mini" @click="linkagesafe"  style="margin-right:150px" >保存</el-button>
                <el-button type="primary" size="mini" @click="linkageclose" style="">取消</el-button>
              </div>
          </div>
      </div>
    </div>
  </div>
</template>
<script>
import {get_chart_sensorlist,get_attr1,get_orderName,get_alarmOption} from '../../../js/public.js';
export default {
  props: [
    "isShow",
    "linkagesData",
  ],
  data () {
    return {
        deviceAlarm:'',//告警设备
        deviceNameList:[],//设备列表
        deviceName:'',//设备名称
        deviceOrderList:[],//设备指令列表
        deviceOrder:'',//设备指令
        linkOrder:'',//联动指令
        //deviceOrderList:[],//联动指令list
        linkAlarm:'',//联动设备
        linkNameList:[],//联动设备名称列表
        linkName:'',//联动设备名称
        linkRelieve:"",//解除设备
        relieveName:'',//解除设备名称
        relieveNameList:[],//解除设备列表
        relieveOrder:"",//结束设备指令
        relieveOrderList:"",//结束设备指令list
        tableData2: [{
          date: '联动设备参数',
          name: '',
        }],
        value:'',
        input:'',
        linknewData:this.linkagesData,
        linkNew:[],
        alarmList:[],//告警设备列表
        alarmNameList:[],//选取告警设备后,告警设备名称列表
        alarmsid:"",//告警设备的sensorid
        alarmName:'',//告警设备的devicdid
        alarmBriefList:[],//告警设备指令列表
        alarmbriefid:'',//告警指令
        ctrlList:[],//联动设备列表
        ctrlNameList:[],//选取联动设备后,联动设备名称列表
        ctrlsid:'',//联动设备的sensorid
        ctrlName:'',//联动设备的devicdid
        ctrlBriefList:[],//联动告警指令列表
        ctrlbriefid:'',//联动指令
        freeList:[],//解除设备列表
        freeNameList:[],//选取解除设备后,解除设备名称列表
        freesid:'',//解除设备的sensorid
        freeName:"",//解除设备的deviceid
        freeBriefList:[],//解除告警指令列表
        freebriefid:''//解除告警指令
    }
  },
  methods: {
    //点击关闭后通知父组件
    linkageclose () {
      this.$emit('on-close');
    },
    //点击保存
    linkagesafe(){
      let that = this
      let freeenable =""; 
      
      if(!this.$data.alarmbriefid){
          that.$message({
              type: 'error',
              message: '告警设备管理栏,信息不完整!'
          });
          return false;
      }

      if(!this.$data.ctrlbriefid){
          that.$message({
              type: 'error',
              message: '联动设备管理栏,信息不完整!'
          });
          return false;
      }

      if(this.$data.freebriefid){//判断解除设备管理栏信息是否完整
          freeenable = true;  
      }else{
          freeenable = false;
      }

      let alarmbrief_arr = this.$data.alarmbriefid.split(",");
      let mid = parseInt(alarmbrief_arr[0]);
      let alarm =parseInt(alarmbrief_arr[1]);

      if(freeenable){
          let msg={"cmd":"CGI_LINKAGE_SET_APPEND","dname":this.$data.alarmName,"mid":mid,"alarm":alarm,"ctrldname":this.$data.ctrlName,"ctrlcid":this.$data.ctrlbriefid,"freeenable":freeenable,"freedname":this.$data.freeName,"freecid":this.$data.freebriefid};

        let alarmOrder=get_attr1(mid)+get_alarmOption(alarm);
        let alarmsidname=get_chart_sensorlist(this.$data.alarmsid);
        let ctrlsidname=get_chart_sensorlist(this.$data.ctrlsid);
        let ctrlOrder=get_orderName(this.$data.ctrlbriefid);
        let freesidname=get_chart_sensorlist(this.$data.freesid);
        let freeOrder=get_orderName(this.$data.freebriefid);  

        let linkmsg = {"alarmsidname":alarmsidname,"dname":this.$data.alarmName,"alarmOrder":alarmOrder,"ctrlsidname":ctrlsidname,"ctrldname":this.$data.ctrlName,"ctrlOrder":ctrlOrder,"freeenable":true,"freesidname":freesidname,"freedname":this.$data.freeName,"freeOrder":freeOrder};

          $.ajax({
            url:"cgi-bin/main.cgi",
            contentType: "application/json;charset=utf-8",
            type:"post",
            cache:false,
            async: false,
            data:JSON.stringify(msg),
            success:function(msg){
                let data = eval("("+msg+")"); 
                if(data.response==true){
                    that.$message({
                        type: 'success',
                        message: '保存成功!'
                    });
                    linkmsg["lid"] = data.lid;
                    that.$emit('save-types',linkmsg);
                }else{
                  that.$message({
                        type: 'error',
                        message: data.errlog
                    });    
                }      
            }
        })
      }else{
          let msg={"cmd":"CGI_LINKAGE_SET_APPEND","dname":this.$data.alarmName,"mid":mid,"alarm":alarm,"ctrldname":this.$data.ctrlName,"ctrlcid":this.$data.ctrlbriefid,"freeenable":freeenable};

        let alarmOrder=get_attr1(mid)+get_alarmOption(alarm);
        let alarmsidname=get_chart_sensorlist(this.$data.alarmsid);
        let ctrlsidname=get_chart_sensorlist(this.$data.ctrlsid);
        let ctrlOrder=get_orderName(this.$data.ctrlbriefid);

        let linkmsg = {"alarmsidname":alarmsidname,"dname":this.$data.alarmName,"alarmOrder":alarmOrder,"ctrlsidname":ctrlsidname,"ctrldname":this.$data.ctrlName,"ctrlOrder":ctrlOrder,"freeenable":false,"freesidname":"-","freedname":"-","freeOrder":"-"};

          $.ajax({
            url:"cgi-bin/main.cgi",
            contentType: "application/json;charset=utf-8",
            type:"post",
            cache:false,
            async: false,
            data:JSON.stringify(msg),
            success:function(msg){
                let data = eval("("+msg+")"); 
                if(data.response==true){
                    that.$message({
                        type: 'success',
                        message: '保存成功!'
                    });
                    linkmsg["lid"] = data.lid;
                    that.$emit('save-types',linkmsg);
                }else{
                  that.$message({
                        type: 'error',
                        message: data.errlog
                    });    
                }      
            }
        })

      }
    },
    clear_alarmname(){//清空设备名称,指令及指令列表
        this.$data.alarmName = "";
        this.$data.alarmbriefid = "";
        this.$data.alarmBriefList = [];
    },
    clear_ctrlname(){
        this.$data.ctrlName="";
        this.$data.ctrlbriefid="";
        this.$data.ctrlBriefList=[];
    },
    clear_freename(){
        this.$data.freeName="";
        this.$data.freebriefid="";
        this.$data.freeBriefList=[];
    },
    //获取告警设备管理
    get_alarm_list(){
        let that = this;
        let msg = {"cmd":"CGI_LINKAGE_GET_UPSIDE_DEVICE"};

        if(window.sessionStorage.getItem('username')=="zyl"){
            this.$ajax.get("./static/get_linkage_alarmlist.json",msg).then(function(res){
              let arr=new Array();
              $.each(res.data.sensorlist,function(i,val){
                   let msg_str = {"sid":val,"sname":get_chart_sensorlist(val)};
                   arr.push(msg_str);
              })
              that.$data.alarmList=arr;      
              that.$data.alarmNameList=res.data;
            }) 
        }else{
            $.ajax({
                url:"cgi-bin/main.cgi",
                contentType: "application/json;charset=utf-8",
                type:"post",
                cache:false,
                async: false,
                data:JSON.stringify(msg),
                success:function(msg){
                    let data = eval("("+msg+")"); 
                    let arr=new Array();
                    $.each(data.sensorlist,function(i,val){
                         let msg_str = {"sid":val,"sname":get_chart_sensorlist(val)};
                         arr.push(msg_str);
                    })
                    that.$data.alarmList=arr;      
                    that.$data.alarmNameList=data;
                }
            }) 

        }
    },
    //获取告警设备指令
    get_alarm_brief(){
        let  that = this;
        this.$data.alarmbriefid = "";
        this.$data.alarmBriefList = [];
        let msg = {"cmd":"CGI_LINKAGE_GET_ALARMPARAM","dname":this.$data.alarmName}

        if(window.sessionStorage.getItem('username')=="zyl"){
            this.$ajax.get("./static/get_alarm_brief.json",msg).then(function(res){
                let  arr = new Array();
                $.each(res.data,function(i,val){
                    
                    if(val['islimit'] == true){ //
                        let up_id = val["id"] +","+ 2;
                        let up_name = get_attr1(val["id"]) + "过高";
                        let up_str = {"alarmbriefid":up_id,"alarmbrief":up_name};
                        let down_id = val["id"] +","+ 3;
                        let down_name = get_attr1(val["id"]) + "过低";
                        let down_str = {"alarmbriefid":down_id,"alarmbrief":down_name}

                        arr.push(up_str);
                        arr.push(down_str);
                    }else{
                        let alarmbriefid = val["id"] +","+ 1;
                        let alarmbriefname =get_attr1(val["id"]); 
                        let alarm_str = {"alarmbriefid":alarmbriefid,"alarmbrief":alarmbriefname}
                        arr.push(alarm_str);
                    }
                })
                that.$data.alarmBriefList = arr
            })
        }else{
            $.ajax({
                url:"cgi-bin/main.cgi",
                contentType: "application/json;charset=utf-8",
                type:"post",
                cache:false,
                async: false,
                data:JSON.stringify(msg),
                success:function(msg){
                    let data = eval("("+msg+")"); 
                    let  arr = new Array();
                    $.each(data,function(i,val){
                        if(val['islimit'] == true){ //
                            let up_id = val["id"] +","+ 2;
                            let up_name = get_attr1(val["id"]) + "过高";
                            let up_str = {"alarmbriefid":up_id,"alarmbrief":up_name};
                            let down_id = val["id"] +","+ 3;
                            let down_name = get_attr1(val["id"]) + "过低";
                            let down_str = {"alarmbriefid":down_id,"alarmbrief":down_name}

                            arr.push(up_str);
                            arr.push(down_str);
                        }else{
                            let alarmbriefid = val["id"] +","+ 1;
                            let alarmbriefname =get_attr1(val["id"]); 
                            let alarm_str = {"alarmbriefid":alarmbriefid,"alarmbrief":alarmbriefname}
                            arr.push(alarm_str);
                        }
                    })
                    that.$data.alarmBriefList = arr
                }
            }) 
        }
     
    },
    //获取联动设备管理
    get_ctrl_list(){
        let that = this;

        let msg = {"cmd":"CGI_LINKAGE_GET_DOWNSIDE_DEVICE"};
        
        if(window.sessionStorage.getItem('username')=="zyl"){
            this.$ajax.get("./static/get_linkage_ctrllist.json",msg).then(function(res){
                  let arr=new Array();
                  $.each(res.data.sensorlist,function(i,val){
                       let msg_str = {"sid":val,"sname":get_chart_sensorlist(val)};
                       arr.push(msg_str);
                  })
                  that.$data.ctrlList=arr;      
                  that.$data.ctrlNameList=res.data;
            })
        }else{
            $.ajax({
                url:"cgi-bin/main.cgi",
                contentType: "application/json;charset=utf-8",
                type:"post",
                cache:false,
                async: false,
                data:JSON.stringify(msg),
                success:function(msg){
                    let data = eval("("+msg+")"); 
                    let arr=new Array();
                    $.each(data.sensorlist,function(i,val){
                         let msg_str = {"sid":val,"sname":get_chart_sensorlist(val)};
                         arr.push(msg_str);
                    })
                    that.$data.ctrlList=arr;      
                    that.$data.ctrlNameList=data;
                }
            })
        } 
    },
    //获取联动设备指令
    get_ctrl_brief(){
        let  that = this;
        this.$data.ctrlbriefid="";
        this.$data.ctrlBriefList=[];
        let msg = {"cmd":"CGI_LINKAGE_GET_DEVICECTRLLIST","dname":this.$data.ctrlName}
        
        if(window.sessionStorage.getItem('username')=="zyl"){
            this.$ajax.get("./static/get_ctrl_brief.json",msg).then(function(res){
                let  arr = new Array();
                $.each(res.data,function(i,val){
                     let ctrl_str = {"ctrlbriefid":val,"ctrlbrief":get_orderName(val)}
                     arr.push(ctrl_str);
                })

                that.$data.ctrlBriefList = arr
            })
        }else{
            $.ajax({
                url:"cgi-bin/main.cgi",
                contentType: "application/json;charset=utf-8",
                type:"post",
                cache:false,
                async: false,
                data:JSON.stringify(msg),
                success:function(msg){
                    let data = eval("("+msg+")"); 
                    let  arr = new Array();
                    $.each(data,function(i,val){
                        let ctrl_str = {"ctrlbriefid":val,"ctrlbrief":get_orderName(val)}
                        arr.push(ctrl_str);
                    })
                    that.$data.ctrlBriefList = arr
                }
            })
        } 
    },
    //获取解除设备列表
    get_free_list(){
        let that = this;
        let msg = {"cmd":"CGI_LINKAGE_GET_DOWNSIDE_DEVICE"};
      
        if(window.sessionStorage.getItem('username')=="zyl"){
            this.$ajax.get("./static/get_linkage_freelist.json",msg).then(function(res){
              let arr=new Array();
              $.each(res.data.sensorlist,function(i,val){
                   let msg_str = {"sid":val,"sname":get_chart_sensorlist(val)};
                   arr.push(msg_str);
              })
              that.$data.freeList=arr;      
              that.$data.freeNameList=res.data;
            })
        }else{
            $.ajax({
                url:"cgi-bin/main.cgi",
                contentType: "application/json;charset=utf-8",
                type:"post",
                cache:false,
                async: false,
                data:JSON.stringify(msg),
                success:function(msg){
                    let data = eval("("+msg+")"); 
                    let arr=new Array();
                    $.each(data.sensorlist,function(i,val){
                         let msg_str = {"sid":val,"sname":get_chart_sensorlist(val)};
                         arr.push(msg_str);
                    })
                    that.$data.freeList=arr;      
                    that.$data.freeNameList=data;
                }
            })
        }
    },
    //获取告警设备指令
    get_free_brief(){
      let  that = this;
      this.$data.freebriefid="";
      this.$data.freeBriefList=[];
      let msg = {"cmd":"CGI_LINKAGE_GET_DEVICECTRLLIST","dname":this.$data.freeName};

      if(window.sessionStorage.getItem('username')=="zyl"){
         this.$ajax.get("./static/get_ctrl_brief.json",msg).then(function(res){
            let  arr = new Array();
            $.each(res.data,function(i,val){
                  let free_str = {"freebriefid":val,"freebrief":get_orderName(val)}
                 arr.push(free_str);
            })
            that.$data.freeBriefList = arr
          })

      }else{
          $.ajax({
              url:"cgi-bin/main.cgi",
              contentType: "application/json;charset=utf-8",
              type:"post",
              cache:false,
              async: false,
              data:JSON.stringify(msg),
              success:function(msg){
                  let data = eval("("+msg+")"); 
                  let  arr = new Array();
                  $.each(data,function(i,val){
                       let free_str = {"freebriefid":val,"freebrief":get_orderName(val)}
                       arr.push(free_str);
                  })
                  that.$data.freeBriefList = arr
              }
          })
      }
    }
  },
  //页面渲染数据挂载时弹窗数据的赋值
  mounted(){
    this.get_alarm_list();
    this.get_ctrl_list();
    this.get_free_list();
  }
}
</script>
<style>
.dialog-wrap {
  position: fixed;
  width: 100%;
  height: 100%;
}
.dialog-cover {
  background: #000;
  opacity: .3;
  position: fixed;
  z-index: 100;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
.linkagepopup-warp .dialog-content {
  width: 700px;
  height: 300px;
  background: #fff;
  overflow: hidden;
  position: fixed;
  left: 50%;
  margin-left: -350px;
  top: 50%;
  margin-top: -150px;
  z-index: 101;
  border-radius: 10px;
}
.linkagepopup-warp .dialog-close {
  position: absolute;
  right: 5px;
  top: 5px;
  width: 20px;
  height: 20px;
  text-align: center;
  cursor: pointer;
}
.dialog-close:hover {
  color: #4fc08d;
}
.linkagepopup-warp .devicepopup{
  width: 700px;
  height: 300px;
  background: #f5f5f5;
  overflow: hidden;
  position: fixed;
  left: 50%;
  margin-left: -350px;
  top: 50%;
  margin-top: -150px;
  z-index: 101;
  border-radius: 10px;
}
.popupheader{
  height: 40px;
  background: #409EFF;
  width: 100%;
  line-height: 40px;
  color: #fff;
  font-weight: 600;
  letter-spacing: 3px;
  text-indent: 30px;
  position: relative;
}
.linkagepopup-warp .popupheader .el-button{
  font-size: 20px;
  height: 25px;
  width: 25px;
  padding: 0px;
  margin: 0px;
  float: right;
  margin-right: 15px;
  margin-top: 7px;
  text-indent: 0px;
}
.linkagepopup-warp .popupcontent{
  height: 220px;
  width: 100%;
  overflow: hidden;
}
.linkagepopup-warp .popupcontent ul li{
  width: 100%;
  height: 40px;
  overflow: hidden;
  margin: 0px;
  background: #fff;
}
.popupfooter{
  height: 40px;
  width: 100%;
  line-height: 40px;
  text-align: center;
}
.linkagepopup-warp .popupfooter .el-button{
  width: 70px;
  letter-spacing: 2px;
  font-size: 12px;
  font-weight: 600
}
.linkagepopup-warp .popupcontent ul{
  width: 100%;
  margin:0px;
  padding:10px 0px;
  height: 100%;
}
.linkagepopup-warp .popupcontent ul li{
  float: left;
  width: 232px;
  height: 100%;
  padding: 0px;
  margin: 0px;
}
.linkagepopup-warp .popupcontent ul li.border{
  border-right: 1px solid #dcdfe6;
}
.linkagepopup-warp .popupcontent-title{
  text-align: center;
  width: 100%;
  height: 50px;
  line-height: 50px;
  letter-spacing: 1px;
}
.linkage-table{
  border: 0px;
  border-left: 1px solid #ddd;
}
.linkage-table td{
  border: 0px;
  padding: 10px 0px
}
.linkage-table::before{
  width: 0px;
}
.linkage-table-left td{
  padding: 18px 0px;
}
.link-popu-box{
  width: 100%;
  height:28px;
  overflow: hidden;
  margin-top: 15px;
}
.link-popu-type .link-popu-box{
  margin-top: 20px;
}
.popupcontent-mian{
  width: 100%;
  height: 200px;
  overflow: hidden;
  width: 249px;
  border-right: 1px solid #dcdfe6;
}
.link-popu-box .popu-box{
  display: block;
  width: 120px;
  height: 28px;
  line-height: 28px;
  text-align: center;
  font-size: 14px;
  float: left;
}
.link-popu-box .el-select,.link-popu-box .el-input{
  width: 100px;
  float: left;
}
</style>